package org.ttitfly.ttcore.json.menu;

import java.util.Comparator;

import org.ttitfly.ttcore.entity.menu.SystemMenu;

@SuppressWarnings("unchecked")
public class MenuSort implements Comparator {

	//排序和去重都在这里实现，如果返回0，则代表同一条记录，不会被添加到TreeSet中
	@Override
	public int compare(Object o1, Object o2) {
		if(o1 instanceof SystemMenu){
			SystemMenu sm1 = (SystemMenu)o1;
			SystemMenu sm2 = (SystemMenu)o2;
			int result = sm1.getOrderNum() - sm2.getOrderNum();
			int nt = 0;
			if(sm1.getId()>sm2.getId()){
				nt = 1;
			}else if(sm1.getId()<sm2.getId()){
				nt = -1;
			}
			return result == 0? nt : result ; 
		}else {
			BaseMenu sm1 = (BaseMenu)o1;
			BaseMenu sm2 = (BaseMenu)o2;
			int result = sm1.getOrderNum() - sm2.getOrderNum();
			int nt = 0;
			if(Long.parseLong(sm1.getId())>Long.parseLong(sm2.getId())){
				nt = 1;
			}else if(Long.parseLong(sm1.getId())<Long.parseLong(sm2.getId())){
				nt = -1;
			}
			return result == 0? nt : result ;  
		}
	}

}
